package export.level
{
    import base.BaseHero;
    import base.BaseLevelListener;
    
    import com.dusk.tool.DataBase;
    
    import com.dusk.tool.HitTest;
    import com.game.data.constant.FrameRate;
    import com.game.data.vo.HitResultVO;
    
    import flash.display.MovieClip;
    import flash.geom.Point;
    
    public class StageListener22 extends BaseLevelListener
    {
        
        public function StageListener22()
        {
            super();
            stoneThornArr = [];
            playInfo = new DataBase();
            playInfo.setDefaultValue(false);
        }
        
        private var stoneThornArr:Array;
        private var stoneThornHurt:int = 30;
        private var playInfo:DataBase;
        public var hurtTimes:int = 0;
        
        override public function start():void
        {
            super.start();
            for (var i:int = 0; i < gc.gameScene.numChildren; i++)
            {
                var mc:MovieClip = gc.gameScene.getChildAt(i) as MovieClip;
                if(mc == null) continue;
                if(mc.getChildByName("trap_stoneThorn") != null)
                {
                    mc.addFrameScript(0,mc.stop);
                    mc.gotoAndStop(1);
                    playInfo[mc.name] = false;
                    stoneThornArr.push(mc);
                }
            }
        }
        
        override public function step():void
        {
            super.step();
            for each (var mc:MovieClip in stoneThornArr)
            {
                if(mc.currentFrame == mc.totalFrames)
                {
                    mc.gotoAndStop(1);
                    playInfo[mc.name] = false;
                }
                else if(playInfo[mc.name])
                {
                    mc.nextFrame();
                }
                renderTrap(mc);
            }
        }
        
        private function renderTrap(thorn:MovieClip):void
        {
            if(thorn.y < 200)
            {
                for each (var bh:BaseHero in gc.team.getHeroArray())
                {
                    if(Math.abs(thorn.x - bh.x) < 250 && !playInfo[thorn.name])
                    {
                        playInfo[thorn.name] = true;
                    }
                }
            }
            for each (bh in gc.team.getHeroArray())
            {
                if(bh.isReadyToDestroy ||
                        bh.isDead() ||
                        bh.isInvincible() ||
                        !HitTest.complexHitTestObject(thorn,bh.getCollideSprite()))
                {
                    continue;
                }
                var vo:HitResultVO = new HitResultVO();
                vo.hurt = stoneThornHurt * (gc.difficulty + 1);
                vo.hitType = 0;
                if(hurtTimes >= 10) vo.hurt *= 10;
                bh.loseHp(vo);
                if(bh.isDead()) return;
                bh.setAction("hurt");
                bh.setAttackBack(new Point(bh.getBBDC().getDirect() * -25,0));
                bh.setStiff(10)
                bh.setInvincible(FrameRate.ANIMATE_FRAME_RATE * 0.5);
                CONFIG::RELEASE
                {
                    hurtTimes++;
                }
            }
        }
        
        override public function destroy():void
        {
            super.destroy();
            stoneThornArr = [];
        }
    }
}
